﻿using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Collections.ObjectModel;

namespace Demos.Puzzles.Polyominoes
{
    /// <summary>
    /// Interaction logic for Editor.xaml
    /// </summary>
    public partial class Editor : UserControl, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public ObservableCollection<ListaDeCasillas> Tablero
        {
            get { return (ObservableCollection<ListaDeCasillas>)GetValue(TableroProperty); }
            set { SetValue(TableroProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Tablero.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TableroProperty =
            DependencyProperty.Register("Tablero", typeof(ObservableCollection<ListaDeCasillas>), typeof(Editor), new UIPropertyMetadata(null));




        public int FilaSeleccionada
        {
            get { return (int)GetValue(FilaSeleccionadaProperty); }
            set { SetValue(FilaSeleccionadaProperty, value); }
        }

        // Using a DependencyProperty as the backing store for FilaSeleccionada.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FilaSeleccionadaProperty =
            DependencyProperty.Register("FilaSeleccionada", typeof(int), typeof(Editor), new UIPropertyMetadata(0));



        public int ColumnaSeleccionada
        {
            get { return (int)GetValue(ColumnaSeleccionadaProperty); }
            set { SetValue(ColumnaSeleccionadaProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ColumnaSeleccionada.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ColumnaSeleccionadaProperty =
            DependencyProperty.Register("ColumnaSeleccionada", typeof(int), typeof(Editor), new UIPropertyMetadata(0));

        public Editor()
        {
            InitializeComponent();
        }

        private void ItemsControlMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var lado = Tablero[0].Lista[0].Lado;
            var distanciaCentroHorizontal = controlTablero.ActualWidth/2;
            var anchuraMediaFila = (Tablero[0].Lista.Count*lado)/2;
            var espacioVacioHorizontal = distanciaCentroHorizontal - anchuraMediaFila;
            ColumnaSeleccionada = (int)((e.GetPosition(controlTablero).X - espacioVacioHorizontal)/lado);
            FilaSeleccionada = (int)((e.GetPosition(controlTablero).Y)/lado);
            NotifyPropertyChanged("FilaSeleccionada");
            NotifyPropertyChanged("ColumnaSeleccionada");
        }
    }
}
